﻿using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;

namespace Common.Framework.Core.Extension
{
    /// <summary>
    /// https://docs.sixlabors.com/articles/imagesharp/index.html?tabs=tabid-1
    /// </summary>
    public static class ImageExtension
    {
        /// <summary>
        /// 缩放图片,保存图片到指定的路径
        /// </summary>
        /// <param name="originFilePath">原图片路径</param>
        /// <param name="newFilePath">缩放后图片路径</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static async Task ResizeAsync(string originFilePath, string newFilePath, int width, int height)
        {
            using (Image image = await Image.LoadAsync(originFilePath))
            {
                image.Mutate(x => x.Resize(width, height));
                await image.SaveAsync(newFilePath);
                image.Dispose();
            }
        }

        /// <summary>
        /// 缩放图片,输出图片流,图片类型为jpg
        /// </summary>
        /// <param name="fileStream">原文件流</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static async Task<Stream> ResizeJpgAsync(Stream fileStream, int width, int height)
        {
            Stream newStream = new MemoryStream();
            using (Image image = await Image.LoadAsync(fileStream))
            {
                image.Mutate(x => x.Resize(width, height));
                await image.SaveAsync(newStream, JpegFormat.Instance);
                image.Dispose();
            }
            //需要设置起始位置,否则会异常
            newStream.Position = 0;
            return newStream;
        }

        /// <summary>
        /// 缩放图片,输出图片流,图片类型为jpg,专为png设置
        /// </summary>
        /// <param name="fileStream">原文件流</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static async Task<Stream> ResizePngAsync(Stream fileStream, int width, int height)
        {
            Stream newStream = new MemoryStream();
            using (Image image = await Image.LoadAsync(fileStream))
            {
                image.Mutate(x => x.Resize(width, height).BackgroundColor(Color.White));
                await image.SaveAsync(newStream, JpegFormat.Instance);
                image.Dispose();
            }
            //需要设置起始位置,否则会异常
            newStream.Position = 0;
            return newStream;
        }

        /// <summary>
        /// 将图片转换为base64字符串
        /// </summary>
        /// <param name="fileStream">原文件流</param>
        /// <returns></returns>
        public static async Task<string> Image2Base64Async(Stream fileStream)
        {
            string base64String;
            using (Image image = await Image.LoadAsync(fileStream))
            {
                base64String = image.ToBase64String(JpegFormat.Instance);
                image.Dispose();
            }
            return base64String;
        }
    }
}